<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<script src="js/mui.min.js"></script>
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			.bh{}
			
		</style>
		<script type="text/javascript" charset="utf-8">
			mui.init({
				swipeBack: true //启用右滑关闭功能
			});

			mui.plusReady(function() {
				window.Url=plus.storage.getItem("Url");
				upList();
				mui('body').on('tap', '.mui-pull-right', function() {
					document.location.href = "setting.html";
				});
				mui('body').on('tap', '.hb', function() {
					plus.storage.setItem("scanbj", this.getAttribute('data-id'));
					document.location.href = "list.html";
				});

			});

			function getinfo() {
				mui(document.getElementById("up")).button('loading');
				mui.ajax(window.Url+'/yj/api.php?a=getStudentInfo', {
					data: {
						"token": plus.storage.getItem("Token")
					},
					crossDomain: false,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						if (data.code == 0) {
							plus.storage.setItem('downLoadStudentInfo', JSON.stringify(data.data));
							getlist();
						} else {
							//alert(plus.storage.getItem("Token"));
							getErrorCodeInfo(data.code);
						}


						//json = JSON.parse(plus.storage.getItem("downLoadStudentInfo"));
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						alert(type);
					}
				});




			}

			function getlist() {
				mui.ajax(window.Url+'/yj/api.php?a=getClassList', {
					data: {
						"token": plus.storage.getItem("Token"),
						"data": ""
					},
					crossDomain: true,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {

						if (data.code == 0) {
							//alert(JSON.stringify(data[0]));
							plus.storage.setItem("classList", JSON.stringify(data.data));

							//mui.alert("已经更新班级信息");
							upList();
							DownScanInfo();

						} else {

							getErrorCodeInfo(data.code);
						}
					},
					error: function(xhr, type, errorThrown) {
						//异常处理；
						alert(type);
					}
				});
			}

			function DownScanInfo() {
				mui.post(window.Url+"/yj/api.php?a=getStudentScan", {
					"token": plus.storage.getItem("Token"),
					"data": ""
				}, function(d) {
					if (d.code == 0) {
						if (d.data == "null") {
							plus.storage.setItem("studentSeat", "{}");
						} else {
							plus.storage.setItem("studentSeat", JSON.stringify(d.data));

						}
						mui.alert(window.Url+"\n已同步数据"); 
					} else {
						getErrorCodeInfo(d.code);
					}
					mui(document.getElementById("up")).button('reset');
				}, 'json')
			}

			function exitApp() {
				if (plus.os.name == "Android") {
					plus.runtime.quit();
				} else {
					outSet("此平台不支持直接退出程序，请按Home键切换应用");
				}
			}

			function upList() {
				if (plus.storage.getItem("classList") == null) {
					mui.alert("请下载数据!");
				} else {
					lis = document.getElementById("bjlist");
					lis.innerHTML = "";
					te = JSON.parse(plus.storage.getItem("classList"));
					mui.each(te, function(k, v) {
						//alert(JSON.stringify(v));
						addOption(v.id, "初" + v.grade + "   " + v.alias + "班");

					});
				}
			}

			function addOption(id, name) {
				lis = document.getElementById("bjlist");
				li = document.createElement("li");
				a = document.createElement('a');
				a.className = "mui-navigate-right hb";
				a.id = "class" + id;
				li.className = "mui-table-view-cell mui-collapse";
				a.innerText = name;
				li.appendChild(a);

				lis.appendChild(li);
				a.setAttribute('data-id', id);

				//lis.innerHTML = lis.innerHTML+"<li class='mui-table-view-cell'><a  class='mui-navigate-right' href='#' onclick=tem("+id+");''>" + name + "</a></li>";
			};

			function clearData() {
				//plus.storage.clear(true);
				plus.storage.setItem("downLoadStudentInfo", "{}");
				plus.storage.setItem("studentSeat", "{}");
				plus.storage.setItem("classList", "{}");
				mui.alert("已经清除数据", "成功");
			}

			function gourl(url) {
				document.location.href = url;
			}

			function DataUp() {
				//alert(plus.storage.getItem("studentSeat"));
				//var window.ubn = document.getElementById("up");

				mui(document.getElementById("up")).button('loading');
				mui.post(window.Url+"/yj/api.php?a=setScanInfo", {
					"token": plus.storage.getItem("Token"),
					"data": plus.storage.getItem("studentSeat")
				}, function(data) {
					//alert(data.e);
					var de = document.getElementById("debug");
					//de.innerHTML = data.debug;  
					//de.innerHTML=JSON.stringify(JSON.parse(plus.storage.getItem("studentSeat")));

					switch (data.code) {
						case 0:
							mui.alert("上传成功！\n新增:" + data.w + "\n忽略:" + data.i, "信息", function() {
								getinfo();
							});
							break;
						case 1:
							mui.alert("无数据上传！", "信息", function() {
								getinfo();
							});
							break;
						case 3:
							mui.alert("数据有误！", "信息");
							break;
						case 4:
							mui.alert("令牌有误！", "信息");
							break;
						default:
							mui.alert("错误！", "信息");
					};
					if (data.code > 1) {
						mui(document.getElementById("up")).button('reset');
					}


				}, 'json')

			}

			function getErrorCodeInfo(code) {
				mui(document.getElementById("up")).button('reset');
				switch (code) {
					case 1:
						mui.alert("无数据上传！", "错误");
						break;
					case 3:
						mui.alert("数据有误！", "错误");
						break;
					case 4:
						mui.alert("令牌有误！", "错误");
						break;
					default:
						mui.alert("错误！", "错误");
				};
			}
		</script>
	</head>
	<body>
		<header id="header" class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<a class="mui-icon mui-icon-bars mui-pull-right" href="setting.html"></a>
			<h1 class="mui-title">新街中学</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded">
				<!-- <button type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="getinfo();">下载数据</button> -->
				<button id="up" type="button" data-loading-text="同步中" class="mui-btn mui-btn-primary mui-btn-block" onclick="DataUp();">同步数据</button>
				<button type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="clearData();">清除数据</button>
				<!-- <button type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="gourl('scan.html');">扫描</button> -->
				<a type="button" class="mui-btn mui-btn-primary mui-btn-block" onclick="exitApp();" href="#">退出</a>

			</div>


			<ul id="list" class="mui-table-view mui-table-view-chevron">
				<li class="mui-table-view-cell mui-collapse">
					<a class="mui-navigate-right" href="#">
						班级列表
					</a>
					<ul id="bjlist" class="mui-table-view mui-table-view-chevron">
						<li class="mui-table-view-cell">
							<a class='mui-navigate-right' href='#'>
								班级列表
							</a>
						</li>
					</ul>
				</li>
			</ul>

		</div>
		<div id="debug"></div>

	</body>
</html>
